package com.healthdata.controller;

import com.healthdata.entity.ComprehensiveHospitalCost;
import com.healthdata.entity.HospitalByBedSize;
import com.healthdata.service.ComprehensiveHospitalCostService;
import com.healthdata.service.HospitalByBedSizeService;
import com.healthdata.vo.HospitalTypeVO;
import com.healthdata.vo.MedicalCostRatioVO;
import com.healthdata.vo.MedicalCostTrendVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/hospital")
@CrossOrigin(origins = "http://localhost:5173")
public class HospitalDataController {

    @Autowired
    private ComprehensiveHospitalCostService costService;

    @Autowired
    private HospitalByBedSizeService bedSizeService;

    private final HospitalByBedSizeService hospitalByBedSizeService;

    public HospitalDataController(HospitalByBedSizeService hospitalByBedSizeService) {
        this.hospitalByBedSizeService = hospitalByBedSizeService;
    }


    /**
     * 获取所有医疗费用数据
     */
    @GetMapping("/cost/all")
    public List<ComprehensiveHospitalCost> getAllCostData() {
        return costService.getAllCostData();
    }

    /**
     * 获取医疗费用趋势数据
     */
    @GetMapping("/cost/trend")
    public MedicalCostTrendVO getMedicalCostTrend() {
        return costService.getMedicalCostTrend();
    }

    /**
     * 获取医疗费用比率数据
     */
    @GetMapping("/cost/ratio")
    public MedicalCostRatioVO getMedicalCostRatio() {
        return costService.getMedicalCostRatio();
    }

    /**
     * 按级别和年份获取医疗费用数据
     */
    @GetMapping("/cost/level/{level}/years/{years}")
    public List<ComprehensiveHospitalCost> getCostDataByLevelAndYear(
            @PathVariable("level") String level,
            @PathVariable("years") String yearsStr) {
        String[] yearsArray = yearsStr.split(",");
        List<Integer> years = new java.util.ArrayList<>();
        for (String year : yearsArray) {
            years.add(Integer.parseInt(year));
        }
        return costService.getCostDataByLevelAndYear(level, years);
    }

    /**
     * 获取所有年份
     */
    @GetMapping("/cost/years")
    public List<Integer> getAllYears() {
        return costService.getAllYears();
    }

    /**
     * 获取所有级别
     */
    @GetMapping("/cost/levels")
    public List<String> getAllLevels() {
        return costService.getAllLevels();
    }

    /**
     * 获取所有床位分布数据
     */
    @GetMapping("/bed/all")
    public List<HospitalByBedSize> getAllBedSizeData() {
        return bedSizeService.getAllBedSizeData();
    }

    /**
     * 获取床位占用率数据
     */
    @GetMapping("/bed/occupancy")
    public List<HospitalTypeVO> getBedOccupancyData() {
        return hospitalByBedSizeService.findHospitalTypes();
    }
}
